package com.rc.saas.mini.controller.area;

import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.model.platform.PlatformArea;
import com.rc.saas.mini.model.platform.PlatformAreaExample;
import com.rc.saas.mini.model.platform.PlatformCity;
import com.rc.saas.mini.model.platform.PlatformCityExample;
import com.rc.saas.mini.service.platform.PlatformAreaService;
import com.rc.saas.mini.service.platform.PlatformCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/5/10
 */
@Controller
@RequestMapping("/area")
@Api(tags = "04.省市区相关")
public class AreaController extends MinipBaseController {
    private static Logger logger = LogManager.getLogger(AreaController.class);

    @Autowired
    private PlatformAreaService platformAreaService;
    @Autowired
    private PlatformCityService platformCityService;

    @ResponseBody
    @RequestMapping(value = "/open/listAreaByCity", method = RequestMethod.GET)
    @ApiOperation(value = "根据市获取区", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityId", paramType = "query", value = "市ID", required = true)
    })
    public DataTResponse<List<PlatformArea>> listAreaByCity(String cityId) {
        PlatformAreaExample example = new PlatformAreaExample();
        example.createCriteria().andCityIdEqualTo(cityId);
        example.setOrderByClause("letter_sort");
        List<PlatformArea> platformAreas = platformAreaService.selectByExample(example);
        return DataTResponse.SUCCESS_DATA(platformAreas);
    }


    @RequestMapping(value = "/open/listCityByProvince", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据省份(不传，查所有)查找城市", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "provinceId", paramType = "query", value = "省份", required = false)
    public DataTResponse<List<PlatformCity>> listCityByProvince(String provinceId) {
        PlatformCityExample example = new PlatformCityExample();
        if (provinceId == null || provinceId.length() == 0) {
            return DataTResponse.SUCCESS_DATA(platformCityService.selectByExample(example));
        }
        example.createCriteria().andProvinceIdEqualTo(provinceId);
        example.setOrderByClause("letter_sort");
        return DataTResponse.SUCCESS_DATA(platformCityService.selectByExample(example));
    }

    @RequestMapping(value = "/open/hostCity", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "热点城市", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public DataTResponse<List<PlatformCity>> hostCity() {
        List<PlatformCity> platformCityList = new ArrayList<>();
        PlatformCity platformCity1 = platformCityService.selectByCityName("深圳");
        PlatformCity platformCity2 = platformCityService.selectByCityName("北京");
        PlatformCity platformCity3 = platformCityService.selectByCityName("上海");
        PlatformCity platformCity4 = platformCityService.selectByCityName("成都");
        PlatformCity platformCity5 = platformCityService.selectByCityName("广州");
        PlatformCity platformCity6 = platformCityService.selectByCityName("杭州");
        PlatformCity platformCity7 = platformCityService.selectByCityName("武汉");
        PlatformCity platformCity8 = platformCityService.selectByCityName("西安");
        platformCityList.add(platformCity1);
        platformCityList.add(platformCity2);
        platformCityList.add(platformCity3);
        platformCityList.add(platformCity4);
        platformCityList.add(platformCity5);
        platformCityList.add(platformCity6);
        platformCityList.add(platformCity7);
        platformCityList.add(platformCity8);

        return DataTResponse.SUCCESS_DATA(platformCityList);
    }


    @RequestMapping(value = "/open/selectByCityName", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "名字查找城市", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "cityName", paramType = "query", value = "名字", required = false)
    public DataTResponse<List<PlatformCity>> selectByCityName(String cityName) {
        PlatformCityExample example = new PlatformCityExample();
        example.createCriteria().andCityNameEqualTo(cityName);
        return DataTResponse.SUCCESS_DATA(platformCityService.selectByExample(example).stream().findFirst().orElseGet(() -> null));
    }

}
